package test.com.sms.selenium.singlepoint.util;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;


/** enter page by Ticketing \ New Incident(Call Center) , create and save record*/
public class SpIncidentEditPageUtil extends SeleniumPageUtil {
	
	/*menus*/
	// sub menu of search 
	public final static By IE_BUTTON_SEARCH = By.id("ctl44_btnSearch");
	// incident ID input text of main section
	public final static By IE_MAIN_INPUT_INCIDENT_ID = By.id("ctl44_txtIncidentID"); 
	// the result incident ID of search
	public final static By IE_LINK_RESULT_INCIDENT_ID = By.xpath("//*[@id='tblResults']/tbody/tr[2]/td[1]/a");
	
	/*edit page main sections data fields */ 
	public final static By IE_BUTTON_MAIN_SAVE = By.xpath("//*[@id='badd6d10-5932-4bbc-b11c-c04a27fe36ac']/a");
	public final static By IE_LBL_CUSTOMER = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[1]/td[1]");
	public final static By IE_SPAN_CUSTOMER = By.id("IncidentHdr1_lblCustomer");
	public final static By IE_LBL_STATUS = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[1]/td[3]");
	public final static By IE_INPUT_STATUS = By.id("IncidentHdr1_txtStatus");
	public final static By IE_LBL_INCIDENT_ID = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[2]/td[1]"); 
	public final static By IE_INPUT_INCIDENT_ID = By.id("IncidentHdr1_txtIncidentNum");
	public final static By IE_LBL_PROBLEM_SUMMARY = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[2]/td[3]");
	public final static By IE_INPUT_PROBLEM_SUMMARY = By.id("IncidentHdr1_txtDescription");
	public final static By IE_LBL_EXTERNAL_TICKET_ID = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[3]/td[1]");
	public final static By IE_INPUT_EXTERNAL_TICKET_ID = By.id("IncidentHdr1_txtExternalTicketID");
	public final static By IE_LBL_INCIDENT_PRIORITY = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[3]/td[3]");
	public final static By IE_INPUT_INCIDENT_PRIORITY = By.id("IncidentHdr1_cboPriorityCodeKey");
	public final static By IE_LBL_SRO_TYPE = By.xpath("//*[@id='IncidentHdr1_rowSmsOnlyToggle1']/td[3]");
	public final static By IE_SELECT_SRO_TYPE = By.id("IncidentHdr1_cboSroType");
	public final static By IE_LBL_INCIDENT_DATE_AND_TIME = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[5]/td[1]");
	public final static By IE_INPUT_INCIDENT_DATE = By.id("IncidentHdr1_txtIncidentDate");
	public final static By IE_INPUT_INCIDENT_TIME = By.id("IncidentHdr1_txtTime");
	public final static By IE_LBL_FOLLOW_UP_TIME = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[5]/td[3]");
	public final static By IE_INPUT_FOLLOW_UP_TIME = By.id("IncidentHdr1_txtFollowUp");
	public final static By IE_LBL_CREATOR_PHONE = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[2]/tbody/tr[6]/td[1]");
	public final static By IE_INPUT_CREATOR = By.id("IncidentHdr1_txtCreateBy");
	public final static By IE_INPUT_PHONE = By.id("IncidentHdr1_txtCreatorPhone");
	public final static By IE_LBL_SERVICE_PARTNER = By.id("IncidentHdr1_lblServicePartner");
	public final static By IE_INPUT_SERVICE_PARTNER = By.id("IncidentHdr1_txtPartner");
	public final static By IE_LBL_REQUESTER = By.xpath("//*[@id='IncidentHdr1_rowCustomerUnknownToggle1']/td[1]");
	public final static By IE_LINK_REQUESTER_EDIT = By.id("IncidentHdr1_btnRequesterEdit");
	public final static By IE_LINK_REQUESTER_NEW = By.id("IncidentHdr1_btnRequesterNew");
	public final static By IE_INPUT_REQUESTER = By.id("IncidentHdr1_txtRequester");
	public final static By IE_UL_SELECTION_REQUESTER = By.id("IncidentHdr1_aceRequester_completionListElem");
	public final static By IE_INPUT_REQUESTER_PHONE = By.id("IncidentHdr1_txtRequesterPhone");
	public final static By IE_LBL_CONTRACT_LINE = By.xpath("//*[@id='IncidentHdr1_rowCustomerUnknownToggle1']/td[3]");
	public final static By IE_SPAN_CONTRACT_LINE = By.id("IncidentHdr1_lblContractLineDisplay");
	public final static By IE_LINK_CONTRACT_LINE_VIEW = By.id("IncidentHdr1_lnkContractLine"); 
	public final static By IE_LBL_POINT_OF_CONTACT = By.xpath("//*[@id='IncidentHdr1_rowCustomerUnknownToggle2']/td[1]");
	public final static By IE_LINK_POINT_OF_CONTACT_EDIT = By.id("IncidentHdr1_btnContactEdit");
	public final static By IE_LINK_POINT_OF_CONTACT_NEW = By.id("IncidentHdr1_btnContactNew");
	public final static By IE_INPUT_POINT_OF_CONTACT = By.id("IncidentHdr1_txtPointOfContact");
	public final static By IE_UL_SELECTION_POINT_OF_CONTACT = By.id("IncidentHdr1_aceContact_completionListElem");
	public final static By IE_INPUT_POINT_OF_CONTACT_PHONE = By.id("IncidentHdr1_txtContactPhone");
	public final static By IE_LBL_SLA_TERMS = By.xpath("//*[@id='IncidentHdr1_rowCustomerUnknownToggle2']/td[3]");
	public final static By IE_SPAN_SLA_TERM_NAME = By.id("IncidentHdr1_lblSLATermName");
	public final static By IE_SPAN_SLA_TERM_VERBOSE = By.id("IncidentHdr1_lblSLATermVerbose");
	public final static By IE_DIV_ASSIGNED_TO_PHONE = By.id("IncidentHdr1_pnlAssignedToLbl");
	public final static By IE_LINK_ASSIGNED_NOTIFY = By.id("IncidentHdr1_btnNotify");
	public final static By IE_INPUT_ASSIGNED_TO = By.id("IncidentHdr1_txtAssignedTo");
	public final static By IE_UL_SELECTION_ASSIGNED_TO = By.id("IncidentHdr1_aceAssignedTo_completionListElem");
	public final static By IE_INPUT_ASSIGNER_TO_PHONE = By.id("IncidentHdr1_txtAssignedToPhone");
	public final static By IE_LINK_ASSIGNED_CLEAR = By.id("IncidentHdr1_imgClearAssignment");
	public final static By IE_LBL_RCA = By.xpath("//*[@id='IncidentHdr1_trAssignedTo']/td[3]");
	public final static By IE_CHECKBOX_RCA = By.id("IncidentHdr1_chkRCAFlag");
	public final static By IE_DIV_PNL_TABLES = By.id("pnlTabs");
	public final static By IE_LINK_TAB_ASSETS = By.id("btnTab1");
	public final static By IE_LINK_TAB_REASONS = By.id("btnTab3");
	public final static By IE_LINK_TAB_EVENTS = By.id("btnTab4");
	public final static By IE_LINK_TAB_SERVICE_LEVEL = By.id("btnTab6");
	public final static By IE_LINK_TAB_SERVICE_ADDRESS = By.id("btnTab7");
	public final static By IE_LINK_TAB_ATTACHMENTS = By.id("btnTab8");
	public final static By IE_LINK_TAB_SRO = By.id("btnTab9");
	public final static By IE_LINK_TAB_NOTES = By.id("btnTab10");
	public final static By IE_LINK_TAB_FEATURES = By.id("btnTab11");
	public final static By IE_IMG_CHANGECUSTOMER = By.id("IncidentHdr1_imgChangeCustomer");
    
    /* Event tab main area elements */
    public final static By IE_LINK_TAB_EVENTS_SAVE = By.id("IncEventEdit1_btnSave");
    public final static By IE_LINK_TAB_EVENTS_DELETE = By.id("IncEventEdit1_btnDelete");
    public final static By IE_LINK_TAB_EVENTS_CLOSE = By.id("IncEventEdit1_btnDone");
	public final static By IE_SELECT_TAB_EVENTS_EVENT_CODE = By.id("IncEventEdit1_cboEventCodeID");
    public final static By IE_INPUT_TAB_EVENTS_EVENT_DATE = By.id("IncEventEdit1_dteEventDate_txtDatePart");
    public final static By IE_IMG_TAB_EVENTS_EVENT_DATE = By.id("IncEventEdit1_dteEventDate_imgDateCalendar");
    public final static By IE_SELECT_TAB_EVENTS_EVENT_HOUR = By.id("IncEventEdit1_dteEventDate_cboHour");
    public final static By IE_SELECT_TAB_EVENTS_EVENT_MINUTE = By.id("IncEventEdit1_dteEventDate_cboMinute");
    public final static By IE_SELECT_TAB_EVENTS_EVENT_AM_PM = By.id("IncEventEdit1_dteEventDate_cboMeridiem");
    public final static By IE_INPUT_TAB_EVENTS_CONTACT = By.id("IncEventEdit1_txtContact");
    public final static By IE_SELECT_TAB_EVENTS_OWNER = By.id("IncEventEdit1_cboActionUserID");
    public final static By IE_TEXTAREA_TAB_EVENTS_NOTES = By.id("IncEventEdit1_txtNotes");
    
    // elements under the incident events at the bottom of the page
    
    public final static By IE_INCIDENT_EVENTS_CODE = By.id("IncEventsList1_IncEventReport_ReportOutput");
    public final static By IE_LINK_INCIDENT_EVENTS_VIEW = By.xpath("//*[@id='IncEventsList1_IncEventReport_ReportOutput']/table/tbody/tr[3]/td[1]/a");
    public final static By IE_TD_INCIDENT_EVENTS_OWNER = By.xpath("//*[@id='IncEventsList1_IncEventReport_ReportOutput']/table/tbody/tr[3]/td[5]");
    
    // Event tab elements labels
    public final static By IE_SPAN_TAB_EVENTS_NEW_EVENTS = By.id("IncEventEdit1_lblHdr");
    public final static By IE_LBL_TAB_EVENTS_EVENT_CODE = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[4]/tbody/tr[1]/td[1]");
    public final static By IE_LBL_TAB_EVENTS_EVENT_DATE = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[4]/tbody/tr[1]/td[3]");
    public final static By IE_LBL_TAB_EVENTS_CONTACT = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[4]/tbody/tr[2]/td[1]");
    public final static By IE_UL_SELECTION_TAB_EVENTS_CONTACT = By.id("IncEventEdit1_aceContact_completionListElem");
    public final static By IE_LBL_TAB_EVENTS_OWNER = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[4]/tbody/tr[2]/td[3]");
    public final static By IE_LBL_TAB_EVENTS_NOTES = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[4]/tbody/tr[3]/td[1]");
    
    /* Event tab report area(Incident Events section)*/
	public final static By IE_SPAN_TAB_EVENTS_INCIDENT_EVENTS = By.id("IncEventsList1_IncEventReport_Header_lblHeader");
	public final static By IE_IMG_TAB_EVENTS_EXPORT_TO_XLS = By.id("IncEventsList1_IncEventReport_Header_imgExcelExport");
	public final static By IE_LBL_TAB_EVENTS_PAGE = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[6]/tbody/tr/td[1]");
	public final static By IE_LBL_TAB_EVENTS_PAGE_INDEX = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[6]/tbody/tr/td[1]");
	public final static By IE_LBL_TAB_EVENTS_PAGE_OF = By.xpath("//*[@id='pnlPage']/table/tbody/tr/td/table[6]/tbody/tr/td[3]");
	public final static By IE_LBL_TAB_EVENTS_PAGE_COUNT = By.id("IncEventsList1_IncEventReport_PagingTop_lblPagingPageCount");
	public final static By IE_IMG_TAB_EVENTS_FIRST = By.id("IncEventsList1_IncEventReport_PagingTop_imgFirst");
	public final static By IE_IMG_TAB_EVENTS_PREVIOUS = By.id("IncEventsList1_IncEventReport_PagingTop_imgPrevious");
	public final static By IE_IMG_TAB_EVENTS_NEXT = By.id("IncEventsList1_IncEventReport_PagingTop_imgNext");
	public final static By IE_IMG_TAB_EVENTS_LAST = By.id("IncEventsList1_IncEventReport_PagingTop_imgLast");
	
	// Incident event report span
	public final static By IE_SPAN_TAB_EVENTS_INCIDENT_EVENT_REPORT = By.id("IncEventsList1_IncEventReport_ReportOutput");
	
	// "filter" Images
	public final static By IE_IMG_TAB_EVENTS_FILTER_CODE = By.id("IncEventsList1_IncEventReport_filter_0");
	public final static By IE_IMG_TAB_EVENTS_FILTER_EVENT_DATE = By.id("IncEventsList1_IncEventReport_filter_1");
	public final static By IE_IMG_TAB_EVENTS_FILTER_CONTACT = By.id("IncEventsList1_IncEventReport_filter_2");
	public final static By IE_IMG_TAB_EVENTS_FILTER_OWNER = By.id("IncEventsList1_IncEventReport_filter_3");
	public final static By IE_IMG_TAB_EVENTS_FILTER_CREATOR = By.id("IncEventsList1_IncEventReport_filter_4");
	public final static By IE_IMG_TAB_EVENTS_FILTER_NOTES = By.id("IncEventsList1_IncEventReport_filter_5");
	
	// Report column indexes
	public final static int IE_TD_TAB_EVENTS_COLUMN_VIEW_IDX = 0;
	public final static int IE_TD_TAB_EVENTS_COLUMN_CODE_IDX = 1;
	public final static int IE_TD_TAB_EVENTS_COLUMN_EVENT_DATE_IDX = 2;
	public final static int IE_TD_TAB_EVENTS_COLUMN_CONTACT_IDX = 3;
	public final static int IE_TD_TAB_EVENTS_COLUMN_OWNER_IDX = 4;
	public final static int IE_TD_TAB_EVENTS_COLUMN_CREATOR_IDX = 5;
	public final static int IE_TD_TAB_EVENTS_COLUMN_NOTES_IDX = 6;
	
	// Column headers
	public final static By IE_TD_TAB_EVENTS_COLUMN_CODE = By.id("IncEventsList1_IncEventReport_sort_0");
	public final static By IE_TD_TAB_EVENTS_COLUMN_EVENT_DATE = By.id("IncEventsList1_IncEventReport_sort_1");
	public final static By IE_TD_TAB_EVENTS_COLUMN_CONTACT = By.id("IncEventsList1_IncEventReport_sort_2");
	public final static By IE_TD_TAB_EVENTS_COLUMN_OWNER = By.id("IncEventsList1_IncEventReport_sort_3");
	public final static By IE_TD_TAB_EVENTS_COLUMN_CREATOR = By.id("IncEventsList1_IncEventReport_sort_4");
	public final static By IE_TD_TAB_EVENTS_COLUMN_NOTES = By.xpath("//*[@id='IncEventsList1_IncEventReport_ReportOutput']/table/tbody/tr[2]/th[7]");
	
	// Event records
	public final static By IE_TR_TAB_EVENTS_RESULT_ROWS = By.xpath("//*[@id='IncEventsList1_IncEventReport_ReportOutput']/table/tbody/tr");
	public final static By IE_TR_TAB_EVENTS_FIRST_RESULT_ROW = By.xpath("//*[@id='IncEventsList1_IncEventReport_ReportOutput']/table/tbody/tr[3]");
	
	// elements of sro tab
	public final static By IE_LINK_INCIDENT_NUMBER = By.id("SRO_hlIncidentNumber");
	public final static By IE_INPUT_SRO_NUMBER = By.id("SRO_txtSroHeaderNumber");
	
	// elements of assets tab
	public final static By IE_LINK_ASSET_NAME = By.id("IncidentAsset1_lnkAssetName");
	
	// elements of filter field
	public final static By IE_INPUT_CONTENT = By.id("IncEventsList1_IncEventReport_txtFilterInput");
	public final static By IE_BUTTON_ENTER = By.id("IncEventsList1_IncEventReport_btnFilterEnter");
	public final static By IE_BUTTON_CANCEL = By.id("IncEventsList1_IncEventReport_btnFilterCancel");
	
	// the incident id of sending to edit customer page
	public final static By IE_INPUT_INCIDENT_ID_VALUE = By.id("IncidentHdr1_txtIncidentID");
	
	public static void waitForEditPageToLoad(){
//		SeleniumUtil.waitOnElements(new By[]{
//				IE_SPAN_CUSTOMER, IE_INPUT_INCIDENT_ID, IE_DIV_PNL_TABLES, 
//				IE_LINK_TAB_EVENTS_SAVE, IE_LINK_TAB_EVENTS_CLOSE, 
//				IE_SPAN_TAB_EVENTS_INCIDENT_EVENTS});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(IE_SPAN_CUSTOMER));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(IE_INPUT_INCIDENT_ID));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(IE_DIV_PNL_TABLES));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(IE_LINK_TAB_EVENTS_SAVE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(IE_LINK_TAB_EVENTS_CLOSE));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(IE_SPAN_TAB_EVENTS_INCIDENT_EVENTS));
	}
	
	/**enter the incident ID, click the link of result and enter into the edit page*/
	public static void enterIncidentID(String incidentID) {
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_INPUT_INCIDENT_NO), incidentID);
		Set<String> before = SeleniumUtil.getWindowHandles();
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_LINK_SEARCH));
		SpSearchPageUtil.waitForResultListToLoadComplete();
		SeleniumUtil.waitForFixedMilliseconds(1000);
//		SeleniumUtil.click(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_LINK_RESULT_INCIDENT_ID));
		Set<String> after = SeleniumUtil.getWindowHandles();
		SeleniumUtil.switchToPromptedWindow(before, after);
	}
	
	public static void newEvent(String eventCode, String contactKey, String contactName, String notes){
		// select event code
		SeleniumUtil.selectByVisibleText(new Select(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_SELECT_TAB_EVENTS_EVENT_CODE)), eventCode);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_INPUT_TAB_EVENTS_CONTACT), contactKey);
		// input contact
		SeleniumUtil.waitPageToLoad(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_UL_SELECTION_TAB_EVENTS_CONTACT));
		SeleniumUtil.click(SeleniumUtil.findElement(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_UL_SELECTION_TAB_EVENTS_CONTACT), By.xpath(".//li[text()='" + contactName + "']")));
		// input notes
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_TEXTAREA_TAB_EVENTS_NOTES), notes);
		// save
		SeleniumUtil.click(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_LINK_TAB_EVENTS_SAVE));
		SpIncidentEditPageUtil.waitForEditPageToLoad();
	}
	
}
